package com.nobody.facepk.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.nobody.facepk.pojo.SessionUser;
import com.nobody.facepk.pojo.SessionUserLocal;

/**
 * 判断玩家是否已经登录，如果未登录则跳转到登陆页面
 * 
 * @author wangjun
 * 
 */
public class ContextPlayerInterceptor extends HandlerInterceptorAdapter {
	private static final Logger log = Logger.getLogger(ContextPlayerInterceptor.class);
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		SessionUser user = (SessionUser)request.getSession().getAttribute("SESSION_USER");
		if(user!=null) {
			SessionUserLocal.set(user);
			System.out.println("good request:"+user.getUserId()+" "+request.getRequestURI());
		}
		else {
			System.out.println("bad request:"+request.getRequestURI());
		}
		
		return super.preHandle(request, response, handler);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		SessionUserLocal.clear();
		super.afterCompletion(request, response, handler, ex);
	}
}
